<?php
namespace addon\cms;

use app\common\controller\AddonBase;

use app\common\dao\AddonInterface;

use tpfcore\Core;

use think\Db;

use \tpfcore\util\Config;

/**
 * Cms
 */
class Cms extends AddonBase implements AddonInterface
{
    private $addonName="cms模块";
    /**
     * 插件安装
     */
    public function addonInstall()
    {
        try{

            $this->addonInstallBefore();

            return [RESULT_SUCCESS, $this->addonName.'安装成功'];

        }catch(\Exception $e){

            throw new \Exception($e->getMessage());

        }
        
    }
    
    /**
     * 插件卸载
     */
    public function addonUninstall()
    {   
        try{

            $this->addonUninstallBefore();

            return [RESULT_SUCCESS, $this->addonName.'卸载成功'];
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }
        
    }
    
    /**
     * 插件基本信息
     */
    public function addonInfo()
    {
        
        return [
            'name' => 'cms', 
            'title' => $this->addonName, 
            'describe' => 'cms模块', 
            'author' => 'yaosean', 
            'version' => '2.3' ,
            'require'=>'>=3.1',
            'depend'=>'ueditor,kindeditor',
            'type'=>'module',
            "config" => false
        ];
    }
    
    /**
     * 插件配置信息
     */
    public function addonConfig()
    {
        return [
            'mimi_program'=>[
                "module"=>[
                  1=>[
                    'title'=>"案例展示",
                    'base_url'=>"../case_item/case_item",
                    'more_url'=>"../case_list/case_list",
                    'web_more_url' => 'case-list.html',
                    'web_base_url' => 'case-item.html',
                    'style'=>1,
                    'limit'=>4,
                    'category_id'=>108,
                    'sort'=>"datetime desc"
                  ],
                  2=>[
                    'title'=>"新闻中心",
                    'base_url'=>"../news_item/news_item",
                    'more_url'=>"../news_list/news_list",
                    'web_more_url' => 'news-list.html',
                    'web_base_url' => 'news-item.html',
                    'style'=>1,
                    'limit'=>4,
                    'category_id'=>108,
                    'sort'=>"datetime desc"
                  ],
                  3=>[
                    'title'=>"视频展示",
                    'base_url'=>"../video_list/video_list",
                    'more_url'=>"../video_list/video_list",
                    'web_more_url' => 'video-list.html',
                    'web_base_url' => 'video-list.html',
                    'style'=>1,
                    'limit'=>4,
                    'category_id'=>108,
                    'sort'=>"datetime desc"
                  ],
                  4=>[
                    'title'=>"相册展示",
                    'base_url'=>"../photo_item/photo_item",
                    'more_url'=>"../photo_list/photo_list",
                    'web_more_url' => 'photo-list.html',
                    'web_base_url' => 'photo-item.html',
                    'style'=>1,
                    'limit'=>4,
                    'category_id'=>108,
                    'sort'=>"datetime desc"
                  ]
                ],
                'config'=>[
                  'url'=>'http://test.tpframe.com/cms/mini_program/chat',
                  'token'=>'tpframe',
                  'encodingAESKey'=>'Afeaga1611AWF6AG163F16G161AWF',
                  "appid"=>"wx2w4f614646161456",
                  "secret"=>"awgawfwawfawgawfawefwafewfa",
                  'mini_api_validate'=>1,
                  'api_time_limit' => 6,
                  'apiKey'=>"",
                  'api_ip_authorize'=>""
                ],
                'setting'=>[
                  'mobile' => '18323008570',
                  'qq' => '510974211',
                  'email' => '510974211@qq.com',
                  'address' => 
                  array (
                    'longitude' => '106.539397',
                    'latitude' => '29.580562',
                  ),
                  'is_comment' => '1',
                  'firstName' => '',
                  'organization' => '',
                  'init' => 
                  array (
                    'about_cid' => '12',
                    'nav_style' =>  1
                  ),
                ]
            ]
          ];
    }
    /**
     * 插件安装前的一些操作
     */
    public function addonInstallBefore(){
        // 安装前要进行的一些sql处理
        try{

            $result=Db::query("SELECT id from `".DB_PREFIX."menu` WHERE name='门户管理'");

            if(empty($result)){
                Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('门户管理', 'cms', 'cms', 'index', '1', '', '1', '', 'th', '3', '0');");

                $parentid=Db::getLastInsID();
            }else{
                $parentid=$result[0]['id'];    
            }

            /* cms管理菜单 */
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('CMS管理', 'cms', 'posts', 'index', '1', '', '1', '', 'cms', '0', '$parentid');");

            $parentid_cms=Db::getLastInsID();

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('分类管理', 'cms', 'category', 'index', '1', '', '1', '', '', '0', '$parentid_cms');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('内容管理', 'cms', 'posts', 'index', '1', '', '1', '', '', '0', '$parentid_cms');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('图片库', 'cms', 'PostsImages', 'index', '1', '', '1', '', '', '0', '$parentid_cms');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('视频库', 'cms', 'PostsVideos', 'index', '1', '', '1', '', '', '0', '$parentid_cms');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('内容回收站', 'cms', 'Recycle', 'index', '1', '', '1', '', '', '0', '$parentid_cms');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('内容评论管理', 'cms', 'PostsComments', 'index', '1', '', '1', '', '', '0', '$parentid_cms');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('单页管理', 'cms', 'Spage', 'index', '1', '', '1', '', '', '0', '$parentid_cms');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('留言管理', 'cms', 'Message', 'index', '1', '', '1', '', '', '0', '$parentid_cms');");    
            /* 频道模型 */
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('频道模型', 'cms', 'module', 'index', '1', '', '1', '', 'model', '0', '$parentid');");

            $parentid_module=Db::getLastInsID();

             Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('自定义表单', 'cms', 'CustomForm', 'index', '1', '', '1', '', '', '0', '$parentid_module');");

            /* 小程序菜单 */
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('微信/小程序管理', 'cms', 'MiniProgram', 'index', '1', '', '1', '', 'weixin', '0', '$parentid');");

            $parentid_miniprog=Db::getLastInsID();

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('首页栏目管理', 'cms', 'MiniColumn', 'index', '1', '', '1', '', '', '0', '$parentid_miniprog');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('首页模块配置', 'cms', 'MiniModule', 'index', '1', '', '1', '', '', '0', '$parentid_miniprog');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('开发配置', 'cms', 'MiniChat', 'index', '1', '', '1', '', '', '0', '$parentid_miniprog');");

             Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('程序配置', 'cms', 'MiniSetting', 'index', '1', '', '1', '', '', '0', '$parentid_miniprog');");

            /* 内容维护菜单 */
            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('内容维护', 'cms', 'PostsMaintain', 'index', '1', '', '1', '', 'map', '0', '$parentid');");

            $parentid_op=Db::getLastInsID();

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('自动提交配置', 'cms', 'posts', 'auto_submit', '1', '', '1', '', '', '0', '$parentid_op');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('内容设置', 'cms', 'posts', 'setting', '1', '', '1', '', '', '0', '$parentid_op');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('内容批量管理', 'cms', 'posts', 'move', '1', '', '1', '', '', '0', '$parentid_op');");

            Db::execute("INSERT INTO `".DB_PREFIX."menu` (`name`, `module`, `controller`, `action`, `type`, `urlext`, `display`, `remark`, `icon`, `sort`, `parentid`) VALUES ('内容批量替换', 'cms', 'posts', 'replace', '1', '', '1', '', '', '0', '$parentid_op');");

            /* 字段升级处理 */
            $table_field_posts=Db::name("Posts")->getFieldsType();
            if(!array_key_exists("keywords", $table_field_posts)){
                Db::query("alter table ".DB_PREFIX."posts add keywords varchar(255);");
            }
            if(!array_key_exists("abstract", $table_field_posts)){
                Db::query("alter table ".DB_PREFIX."posts add abstract varchar(255);");
            }
            if(!array_key_exists("tag", $table_field_posts)){
                Db::query("alter table ".DB_PREFIX."posts add tag varchar(255);");
            }
            if(!array_key_exists("channel", $table_field_posts)){
                Db::query("alter table ".DB_PREFIX."posts add channel tinyint(4) DEFAULT '1' COMMENT '频道 1文章 2图片 3视频';");
            }
            $table_field_posts_comments=Db::name("PostsComments")->getFieldsType();
            if(!array_key_exists("user_id", $table_field_posts_comments)){
                Db::query("alter table ".DB_PREFIX."posts_comments add user_id int(11) DEFAULT '0' COMMENT '用户id';");
            }

            // 初始化配置
            $config = $this->addonConfig();

            Config::mult_array_merge(APP_PATH."extra/config.php",$config);
            
        }catch(\Exception $e){
            throw new \Exception("sql 语句执行错误".$e->getMessage());
        }
        
    }
    /**
     * 卸载前的一些操作
     */
    public function addonUninstallBefore(){
        try{
            $result=Db::query("SELECT id from `".DB_PREFIX."menu` WHERE name='门户管理'");
            if(!empty($result)){
                $parentid=$result[0]['id'];

                $result_id=Db::query("SELECT id from `".DB_PREFIX."menu` WHERE parentid=$parentid");

                foreach ($result_id as $key => $value) {

                    $parentid =$value['id'];

                    Db::execute("DELETE FROM `".DB_PREFIX."menu` WHERE parentid=$parentid or id=$parentid");

                }
                
            }
        }catch(\Exception $e){
            throw new \Exception("sql 语句执行错误".$e->getMessage());
        }
    }
}
